package com.estate.web.controller.report.report;

import com.estate.common.core.domain.AjaxResult;
import com.estate.system.domain.CustomGeocodingResult;
import com.estate.system.domain.Listing;
import com.estate.system.domain.SuburbGeocoding;
import com.estate.system.service.GoogleGeocodingService;
import com.estate.system.service.impl.DataListingsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/geocoding")
public class GeocodingController {

    @Autowired
    private GoogleGeocodingService geocodingService;
    @Resource
    private DataListingsServiceImpl listingsService;

    @PostMapping("/batchProcess")
    public AjaxResult batchProcessListings() {
        try {
            // 根据listingIds查询房源数据
            List<Listing> listings = listingsService.selectListingsAll();
            List<SuburbGeocoding> results = geocodingService.batchGeocodeListings(listings);
            return AjaxResult.success("批量地理编码处理完成", results);
        } catch (Exception e) {
            return AjaxResult.error("处理失败: " + e.getMessage());
        }
    }

    @GetMapping("/suburb/{suburb}")
    public AjaxResult geocodeSuburb(@PathVariable String suburb) {
        try {
            CustomGeocodingResult result = geocodingService.geocodeSuburb(suburb, "NSW", "AU");
            return AjaxResult.success(result);
        } catch (Exception e) {
            return AjaxResult.error("地理编码失败: " + e.getMessage());
        }
    }
}